<template><div><h2 id="应用主体" tabindex="-1"><a class="header-anchor" href="#应用主体"><span>应用主体</span></a></h2>
<p>应用主体是管理 Yii 应用系统的整体结构和生命周期的对象。每个 Yii 应用系统只能包含一个应用主体，应用主体在 <a href="https://learnku.com/docs/yii-framework/2.0.x/entry-script/11999" target="_blank" rel="noopener noreferrer">入口脚本</a> 中创建并能通过表达式 <code v-pre>\Yii::$app</code> 在全局范围内访问。</p>
<blockquote>
<p>Info: 当我们说“一个应用”，它可能是一个应用主体对象，也可能是一个应用系统，是根据上下文来决定[译：中文为避免歧义，Application 翻译为应用主体]。</p>
</blockquote>
<p>Yii 有两种应用主体: <code v-pre>yii\web\Application</code>（WEB 应用主体）和 <code v-pre>yii\console\Application</code>（控制台应用主体），顾名思义，前者主要处理 WEB 请求，后者处理控制台命令行请求，他们都继承父类 <code v-pre>yii\base\Application</code>。</p>
<h2 id="应用主体生命周期" tabindex="-1"><a class="header-anchor" href="#应用主体生命周期"><span>应用主体生命周期</span></a></h2>
<p><img src="https://cdn.learnku.com/uploads/images/202201/30/93088/gwpaXFZhEo.png!large" alt="Application Lifecycle"></p>
<p>当运行 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-entry-scripts.md" target="_blank" rel="noopener noreferrer">入口脚本</a> 处理请求时， 应用主体会经历以下生命周期:</p>
<ol>
<li>入口脚本加载应用主体配置数组。</li>
<li>入口脚本创建一个应用主体实例：</li>
</ol>
<ul>
<li>
<p>调用 [[yii\base\Application::preInit()|preInit()]] 配置几个高级别应用主体属性， 比如 [[yii\base\Application::basePath|basePath]]。</p>
</li>
<li>
<p>注册 [[yii\base\Application::errorHandler|error handler]] 错误处理方法。</p>
</li>
<li>
<p>配置应用主体属性。</p>
</li>
<li>
<p>调用 [[yii\base\Application::init()|init()]] 初始化，该函数会调用 [[yii\base\Application::bootstrap()|bootstrap()]] 运行引导启动组件，具体如下：</p>
<ul>
<li>加载扩展清单文件 <code v-pre>vendor/yiisoft/extensions.php</code>。</li>
<li>创建并运行各个扩展声明的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-extensions.md#bootstrapping-classes" target="_blank" rel="noopener noreferrer">引导组件（bootstrap components）</a>。</li>
<li>创建并运行各个 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-application-components.md" target="_blank" rel="noopener noreferrer">应用组件</a> 以及在应用的 <a href="#29a95a">bootstrap 属性</a> 中声明的各个 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-modules.md" target="_blank" rel="noopener noreferrer">模块（modules）组件</a>（如果有）。</li>
</ul>
</li>
</ul>
<ol start="3">
<li>入口脚本调用 [[yii\base\Application::run()]] 运行应用主体:</li>
</ol>
<ul>
<li>触发 [[yii\base\Application::EVENT_BEFORE_REQUEST|EVENT_BEFORE_REQUEST]] 事件。</li>
<li>处理请求：解析请求 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/runtime-routing.md" target="_blank" rel="noopener noreferrer">路由</a> 和相关参数； 创建路由指定的模块、控制器和动作对应的类，并运行动作。</li>
<li>触发 [[yii\base\Application::EVENT_AFTER_REQUEST|EVENT_AFTER_REQUEST]] 事件。</li>
<li>发送响应到终端用户。</li>
</ul>
<ol start="4">
<li>入口脚本接收应用主体传来的退出状态并完成请求的处理。</li>
</ol>
<h2 id="应用主体配置" tabindex="-1"><a class="header-anchor" href="#应用主体配置"><span>应用主体配置</span></a></h2>
<p>如下所示，当入口脚本创建了一个应用主体， 它会加载一个 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-configurations.md" target="_blank" rel="noopener noreferrer">配置</a> 文件并传给应用主体。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">require</span> <span class="token constant">__DIR__</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'/../vendor/autoload.php'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">require</span> <span class="token constant">__DIR__</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'/../vendor/yiisoft/yii2/Yii.php'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 加载应用主体配置</span></span>
<span class="line"><span class="token variable">$config</span> <span class="token operator">=</span> <span class="token keyword">require</span> <span class="token constant">__DIR__</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'/../config/web.php'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 实例化应用主体、配置应用主体</span></span>
<span class="line"><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>Application</span><span class="token punctuation">(</span><span class="token variable">$config</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>类似其他 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-configurations.md" target="_blank" rel="noopener noreferrer">配置</a> 文件, 应用主体配置文件标明如何设置应用对象初始属性。 由于应用主体配置比较复杂，一般保存在多个类似如上 <code v-pre>config/web.php</code> 的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-configurations.md#configuration-files" target="_blank" rel="noopener noreferrer">配置文件</a> 当中。</p>
<h2 id="应用主体属性" tabindex="-1"><a class="header-anchor" href="#应用主体属性"><span>应用主体属性</span></a></h2>
<p>应用主体配置文件中有许多重要的属性要配置，这些属性指定应用主体的运行环境。 比如，应用主体需要知道如何加载 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-controllers.md" target="_blank" rel="noopener noreferrer">控制器</a> ， 临时文件保存到哪儿等等。 以下我们简述这些属性。</p>
<h3 id="必要属性" tabindex="-1"><a class="header-anchor" href="#必要属性"><span>必要属性</span></a></h3>
<p>在一个应用中，至少要配置2个属性: [[yii\base\Application::id]] 和 [[yii\base\Application::basePath]]。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$config</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'basic'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'basePath'</span> <span class="token operator">=></span> <span class="token function">dirname</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="id-应用唯一标识" tabindex="-1"><a class="header-anchor" href="#id-应用唯一标识"><span>id（应用唯一标识）</span></a></h4>
<p>[[yii\base\Application::id]] 属性用来区分其他应用的唯一标识ID。主要给程序使用。为了方便协作，最好使用数字作为应用主体ID，但不强制要求为数字。</p>
<h4 id="basepath-应用根目录" tabindex="-1"><a class="header-anchor" href="#basepath-应用根目录"><span>basePath（应用根目录）</span></a></h4>
<p>[[yii\base\Application::basePath]] 属性指定该应用的根目录。根目录包含应用系统所有受保护的源代码。在根目录下可以看到对应 MVC 设计模式的<code v-pre>models</code>， <code v-pre>views</code>，<code v-pre>controllers</code> 等子目录。</p>
<p>可以使用路径或 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-aliases.md" target="_blank" rel="noopener noreferrer">路径别名</a> 来在配置 basePath 属性。两种格式所对应的目录都必须存在，否则系统会抛出一个异常。系统会使用 <code v-pre>realpath()</code> 函数规范化配置的路径。</p>
<p>basePath 属性经常用于派生一些其他重要路径（如 runtime 路径），因此，系统预定义 <code v-pre>@app</code> 代表这个路径。派生路径可以通过这个别名组成（如 <code v-pre>@app/runtime</code> 代表 runtime 的路径）。</p>
<h3 id="重要属性" tabindex="-1"><a class="header-anchor" href="#重要属性"><span>重要属性</span></a></h3>
<p>本小节所描述的属性通常需要设置，因为不同的应用属性不同。</p>
<h4 id="aliases-路径别名组" tabindex="-1"><a class="header-anchor" href="#aliases-路径别名组"><span>aliases（路径别名组）</span></a></h4>
<p>[[yii\base\Application::aliases]] 属性允许你用一个数组定义多个别名。数组的key为别名名称，值为对应的路径。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'aliases'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'@name1'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'path/to/path1'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'@name2'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'path/to/path2'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>使用这个属性来定义别名， 代替 [[Yii::setAlias()]] 方法来设置。</p>
<h4 id="bootstrap-启动引导" tabindex="-1"><a class="header-anchor" href="#bootstrap-启动引导"><span>bootstrap（启动引导）</span></a></h4>
<p>这个属性很实用，它允许你用数组指定启动阶段 [[yii\base\Application::bootstrap()]] 需要运行的组件。比如，如果你希望一个 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-modules.md" target="_blank" rel="noopener noreferrer">模块</a> 自定义 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/runtime-url-handling.md" target="_blank" rel="noopener noreferrer">URL 规则</a>，你可以将模块 ID 加入到 bootstrap 数组中。</p>
<p>属性中的每个组件需要指定以下一项:</p>
<ul>
<li>应用 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-applications.md#components" target="_blank" rel="noopener noreferrer">组件</a> ID.</li>
<li><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-applications.md#modules" target="_blank" rel="noopener noreferrer">模块</a> ID.</li>
<li>类名.</li>
<li>配置数组.</li>
<li>创建并返回一个组件的无名称函数.</li>
</ul>
<p>例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// 应用组件ID或模块ID</span></span>
<span class="line">        <span class="token string single-quoted-string">'demo'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 类名</span></span>
<span class="line">        <span class="token string single-quoted-string">'app\components\Profiler'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 配置数组</span></span>
<span class="line">        <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app\components\Profiler'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'level'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 匿名函数</span></span>
<span class="line">        <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">app<span class="token punctuation">\</span>components<span class="token punctuation">\</span>Profiler</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Info: 如果模块 ID 和应用组件 ID 同名，优先使用应用组件 ID，如果你想用模块 ID，可以使用如下匿名函数返回模块 ID。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token function">getModule</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></blockquote>
<p>在启动阶段，每个组件都会实例化。如果组件类实现接口 [[yii\base\BootstrapInterface]]，也会调用 [[yii\base\BootstrapInterface::bootstrap()]] 方法。</p>
<p>举一个实际的例子，<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/start-installation.md" target="_blank" rel="noopener noreferrer">基础应用程序模板</a> 应用主体配置中，开发环境下会在启动阶段运行 <code v-pre>debug</code> 和 <code v-pre>gii</code> 模块。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">YII_ENV_DEV</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// configuration adjustments for 'dev' environment</span></span>
<span class="line">    <span class="token variable">$config</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'bootstrap'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'debug'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$config</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'modules'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'debug'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'yii\debug\Module'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$config</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'bootstrap'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'gii'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$config</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'modules'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'gii'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'yii\gii\Module'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>启动太多的组件会降低系统性能，因为每次请求都需要重新运行启动组件，因此，谨慎配置启动组件。</p>
<p>因为，引导工作必须在处理<strong>每一次</strong>请求之前都进行一遍，因此，让该过程尽可能轻量化就显得非常重要，请尽可能地优化这一步骤。</p>
<p>请尽量不要注册太多引导组件。只有他需要在 HTTP 请求处理的全部生命周期中都作用时才需要使用它。 举一个用到它的范例：一个模块需要注册额外的 URL 解析规则，就应该把它列在应用的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-applications.md#bootstrap" target="_blank" rel="noopener noreferrer">bootstrap 属性</a> 之中，这样该 URL 解析规则才能在解析请求之前生效。（译注：换言之，为了性能需要，除了 URL 解析等少量操作之外，绝大多数组件都应该按需加载，而不是都放在引导过程中。）</p>
<p>在生产环境中，可以开启字节码缓存，比如 APC，来进一步最小化加载和解析 PHP 文件所需的时间。</p>
<p>一些大型应用都包含有非常复杂的应用<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-configurations.md" target="_blank" rel="noopener noreferrer">配置</a>，它们会被分割到许多更小的配置文件中。此时，可以考虑将整个配置数组缓存起来，并在入口脚本创建应用实例之前直接从缓存中加载。</p>
<h4 id="catchall-处理指定请求" tabindex="-1"><a class="header-anchor" href="#catchall-处理指定请求"><span>catchAll（处理指定请求）</span></a></h4>
<p>[[yii\web\Application::catchAll]] 属性仅 [[yii\web\Application]] 网页应用支持。它指定一个要处理所有用户请求的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-controllers.md" target="_blank" rel="noopener noreferrer">控制器方法</a>，通常在维护模式下使用，同一个方法处理所有用户请求。</p>
<p>该配置为一个数组，第一项指定动作的路由，剩下的数组项( key-value 成对)指定传递给动作的参数，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'catchAll'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'offline/notice'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'param1'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value1'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'param2'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value2'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Info: 当开启这个属性时，开发环境下的调试面板将不能工作。</p>
</blockquote>
<h4 id="components-组件配置" tabindex="-1"><a class="header-anchor" href="#components-组件配置"><span>components（组件配置）</span></a></h4>
<p>[[yii\base\Application::components]] 是最重要的属性，它允许你注册多个在其他地方使用的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-applications.md#structure-application-components.md" target="_blank" rel="noopener noreferrer">应用组件</a>。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'cache'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\caching\FileCache'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'identityClass'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app\models\User'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'enableAutoLogin'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>每一个应用组件指定一个 key-value 对的数组，key 代表组件 ID，value 代表组件类名或 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-configurations.md" target="_blank" rel="noopener noreferrer">配置</a>。</p>
<p>在应用中可以任意注册组件，并可以通过表达式 <code v-pre>\Yii::$app-&gt;ComponentID</code> 全局访问。</p>
<p>详情请阅读 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-application-components.md" target="_blank" rel="noopener noreferrer">应用组件</a> 一节.</p>
<h4 id="controllermap-控制器id配置映射" tabindex="-1"><a class="header-anchor" href="#controllermap-控制器id配置映射"><span>controllerMap（控制器ID配置映射）</span></a></h4>
<p>[[yii\base\Application::controllerMap]] 属性允许你指定一个控制器 ID 到任意控制器类。 Yii 遵循一个默认的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-applications.md#controllerNamespace" target="_blank" rel="noopener noreferrer">规则</a> 指定控制器 ID 到任意控制器类（如 <code v-pre>post</code> 对应<code v-pre>app\controllers\PostController</code>）。 通过配置这个属性，可以打破这个默认规则，在下面的例子中， <code v-pre>account</code>对应到<code v-pre>app\controllers\UserController</code>， <code v-pre>article</code> 对应到 <code v-pre>app\controllers\PostController</code>。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'controllerMap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'account'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app\controllers\UserController'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'article'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app\controllers\PostController'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'enableCsrfValidation'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>数组的键代表控制器ID， 数组的值代表对应的类名。</p>
<h4 id="controllernamespace-控制器类默认命名空间" tabindex="-1"><a class="header-anchor" href="#controllernamespace-控制器类默认命名空间"><span>controllerNamespace（控制器类默认命名空间）</span></a></h4>
<p>[[yii\base\Application::controllerNamespace]] 属性指定控制器类默认的命名空间，默认为<code v-pre>app\controllers</code>。 比如控制器ID为 <code v-pre>post</code> 默认对应 <code v-pre>PostController</code>（不带命名空间），类全名为 <code v-pre>app\controllers\PostController</code>。</p>
<p>控制器类文件可能放在这个命名空间对应目录的子目录下，例如，控制器 ID <code v-pre>admin/post</code> 对应的控制器类全名为 <code v-pre>app\controllers\admin\PostController</code>。</p>
<p>控制器类全面能被 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-autoloading.md" target="_blank" rel="noopener noreferrer">自动加载</a>， 这点是非常重要的，控制器类的实际命名空间对应这个属性， 否则，访问时你会收到“Page Not Found”。</p>
<p>如果你想打破上述的规则，可以配置 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-applications.md#controllerMap" target="_blank" rel="noopener noreferrer">controllerMap</a> 属性。</p>
<h4 id="language-应用展示语言" tabindex="-1"><a class="header-anchor" href="#language-应用展示语言"><span>language（应用展示语言）</span></a></h4>
<p>[[yii\base\Application::language]] 属性指定应用展示给终端用户的语言，默认为 <code v-pre>en</code> 标识英文。如果需要之前其他语言可以配置该属性。</p>
<p>该属性影响各种 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/tutorial-i18n.md" target="_blank" rel="noopener noreferrer">国际化</a> ，包括信息翻译、日期格式、数字格式等。例如 [[yii\jui\DatePicker]] 小部件会根据该属性展示对应语言的日历以及日期格式。</p>
<p>推荐遵循 <a href="http://en.wikipedia.org/wiki/IETF_language_tag" target="_blank" rel="noopener noreferrer">IETF language tag</a> 来设置语言，例如 <code v-pre>en</code> 代表英文，<code v-pre>en-US</code> 代表英文(美国).</p>
<p>该属性的更多信息可参考 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/tutorial-i18n.md" target="_blank" rel="noopener noreferrer">国际化</a> 一节.</p>
<h4 id="modules-模块配置" tabindex="-1"><a class="header-anchor" href="#modules-模块配置"><span>modules（模块配置）</span></a></h4>
<p>[[yii\base\Application::modules]] 属性指定应用所包含的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-modules.md" target="_blank" rel="noopener noreferrer">模块</a>。</p>
<p>该属性使用数组包含多个模块类 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-configurations.md" target="_blank" rel="noopener noreferrer">配置</a>，数组的键为模块ID，例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'modules'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// "booking" 模块以及对应的类</span></span>
<span class="line">        <span class="token string single-quoted-string">'booking'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app\modules\booking\BookingModule'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// "comment" 模块以及对应的配置数组</span></span>
<span class="line">        <span class="token string single-quoted-string">'comment'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app\modules\comment\CommentModule'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'db'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'db'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>更多详情请参考 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-modules.md" target="_blank" rel="noopener noreferrer">模块</a> 一节。</p>
<h4 id="name-应用名称" tabindex="-1"><a class="header-anchor" href="#name-应用名称"><span>name（应用名称）</span></a></h4>
<p>[[yii\base\Application::name]] 属性指定你可能想展示给终端用户的应用名称， 不同于需要唯一性的 [[yii\base\Application::id|id]] 属性， 该属性可以不唯一，该属性用于显示应用的用途。</p>
<p>如果其他地方的代码没有用到，可以不配置该属性。</p>
<h4 id="params-参数数组" tabindex="-1"><a class="header-anchor" href="#params-参数数组"><span>params（参数数组）</span></a></h4>
<p>[[yii\base\Application::params]] 属性为一个数组，指定可以全局访问的参数， 代替程序中硬编码的数字和字符， 应用中的参数定义到一个单独的文件并随时可以访问是一个好习惯。 例如用参数定义缩略图的长宽如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'params'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'thumbnail.size'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token number">128</span><span class="token punctuation">,</span> <span class="token number">128</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然后简单的使用如下代码即可获取到你需要的长宽参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$size</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">params</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'thumbnail.size'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$width</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">params</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'thumbnail.size'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>以后想修改缩略图长宽， 只需要修改该参数而不需要相关的代码。</p>
<h4 id="sourcelanguage-应用源语言" tabindex="-1"><a class="header-anchor" href="#sourcelanguage-应用源语言"><span>sourceLanguage（应用源语言）</span></a></h4>
<p>[[yii\base\Application::sourceLanguage]] 属性指定应用代码的语言，默认为 <code v-pre>'en-US'</code> 标识英文（美国），如果应用不是英文请修改该属性。</p>
<p>和 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-applications.md#language" target="_blank" rel="noopener noreferrer">language</a> 属性类似，配置该属性需遵循 <a href="http://en.wikipedia.org/wiki/IETF_language_tag" target="_blank" rel="noopener noreferrer">IETF language tag</a>. 例如 <code v-pre>en</code> 代表英文， <code v-pre>en-US</code> 代表英文(美国)。</p>
<p>该属性的更多信息可参考 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/tutorial-i18n.md" target="_blank" rel="noopener noreferrer">国际化</a> 一节.</p>
<h4 id="timezone-时区" tabindex="-1"><a class="header-anchor" href="#timezone-时区"><span>timeZone（时区）</span></a></h4>
<p>[[yii\base\Application::timeZone]] 属性提供一种方式修改 PHP 运行环境中的默认时区，配置该属性本质上就是调用 PHP 函数 <a href="https://www.php.net/manual/zh/function.date-default-timezone-set.php" target="_blank" rel="noopener noreferrer">date_default_timezone_set()</a>，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'timeZone'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'PRC'</span><span class="token punctuation">,</span><span class="token comment">// 中国</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>有关设置时区含义的更多详细信息，请查看<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/output-formatting.md#time-zones" target="_blank" rel="noopener noreferrer">关于日期格式的部分</a>。</p>
<h4 id="version-应用版本" tabindex="-1"><a class="header-anchor" href="#version-应用版本"><span>version（应用版本）</span></a></h4>
<p>[[yii\base\Application::version]] 属性指定应用的版本，默认为 <code v-pre>'1.0'</code>， 其他代码不使用的话可以不配置。</p>
<h3 id="实用属性" tabindex="-1"><a class="header-anchor" href="#实用属性"><span>实用属性</span></a></h3>
<p>本小节描述的属性不经常设置，通常使用系统默认值。 如果你想改变默认值，可以配置这些属性。</p>
<h4 id="charset-字符集" tabindex="-1"><a class="header-anchor" href="#charset-字符集"><span>charset（字符集）</span></a></h4>
<p>[[yii\base\Application::charset]] 属性指定应用使用的字符集，默认值为 <code v-pre>'UTF-8'</code>， 绝大部分应用都在使用，除非已有的系统大量使用非unicode数据才需要更改该属性。</p>
<h4 id="defaultroute-默认路由" tabindex="-1"><a class="header-anchor" href="#defaultroute-默认路由"><span>defaultRoute（默认路由）</span></a></h4>
<p>[[yii\base\Application::defaultRoute|defaultRoute]] 属性指定未配置的请求的响应 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/runtime-routing.md" target="_blank" rel="noopener noreferrer">路由</a> 规则， 路由规则可能包含模块 ID，控制器 ID，动作 ID。 例如 <code v-pre>help</code>，<code v-pre>post/create</code>，<code v-pre>admin/post/create</code>，如果动作 ID 没有指定， 会使用 [[yii\base\Controller::defaultAction]] 中指定的默认值。</p>
<p>对于 [[yii\web\Application]] 网页应用， 默认值为 <code v-pre>'site'</code> 对应 <code v-pre>SiteController</code> 控制器，并使用默认的动作。 因此你不带路由的访问应用，默认会显示 <code v-pre>app\controllers\SiteController::actionIndex()</code> 的结果。</p>
<p>对于 [[yii\console\Application]] 控制台应用， 默认值为 <code v-pre>'help'</code> 对应 [[yii\console\controllers\HelpController::actionIndex()]]。 因此，如果执行的命令不带参数，默认会显示帮助信息。</p>
<h4 id="extensions-扩展配置" tabindex="-1"><a class="header-anchor" href="#extensions-扩展配置"><span>extensions（扩展配置）</span></a></h4>
<p>[[yii\base\Application::extensions]] 属性用数组列表指定应用安装和使用的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-extensions.md" target="_blank" rel="noopener noreferrer">扩展</a>， 默认使用 <code v-pre>@vendor/yiisoft/extensions.php</code> 文件返回的数组。 当你使用 <a href="https://getcomposer.org/" target="_blank" rel="noopener noreferrer">Composer</a> 安装扩展，<code v-pre>extensions.php</code> 会被自动生成和维护更新。 所以大多数情况下，不需要配置该属性。</p>
<p>特殊情况下你想自己手动维护扩展，可以参照如下配置该属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'extensions'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'extension name'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'version'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'version number'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'BootstrapClassName'</span><span class="token punctuation">,</span>  <span class="token comment">// 可选配，可为配置数组</span></span>
<span class="line">            <span class="token string single-quoted-string">'alias'</span> <span class="token operator">=></span> <span class="token punctuation">[</span>  <span class="token comment">// 可选配</span></span>
<span class="line">                <span class="token string single-quoted-string">'@alias1'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'to/path1'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'@alias2'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'to/path2'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// ... 更多像上面的扩展 ...</span></span>
<span class="line"></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如上所示，该属性包含一个扩展定义数组，每个扩展为一个包含 <code v-pre>name</code> 和 <code v-pre>version</code> 项的数组。 如果扩展要在 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/runtime-bootstrapping.md" target="_blank" rel="noopener noreferrer">引导启动</a> 阶段运行， 需要配置 <code v-pre>bootstrap</code> 以及对应的引导启动类名或 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-configurations.md" target="_blank" rel="noopener noreferrer">configuration</a> 数组。 扩展也可以定义 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-aliases.md" target="_blank" rel="noopener noreferrer">别名</a></p>
<h4 id="layout-默认布局" tabindex="-1"><a class="header-anchor" href="#layout-默认布局"><span>layout（默认布局）</span></a></h4>
<p>[[yii\base\Application::layout]] 属性指定渲染 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-views.md" target="_blank" rel="noopener noreferrer">视图</a> 默认使用的布局名字， 默认值为 <code v-pre>'main'</code> 对应 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-applications.md#layoutPath" target="_blank" rel="noopener noreferrer">布局路径</a>下的 <code v-pre>main.php</code> 文件， 如果 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-applications.md#layoutPath" target="_blank" rel="noopener noreferrer">布局路径</a> 和 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-applications.md#viewPath" target="_blank" rel="noopener noreferrer">视图路径</a> 都是默认值， 默认布局文件可以使用路径别名 <code v-pre>@app/views/layouts/main.php</code></p>
<p>如果不想设置默认布局文件，可以设置该属性为 <code v-pre>false</code>，这种做法比较罕见。</p>
<h4 id="layoutpath-默认布局路径" tabindex="-1"><a class="header-anchor" href="#layoutpath-默认布局路径"><span>layoutPath（默认布局路径）</span></a></h4>
<p>[[yii\base\Application::layoutPath]] 属性指定查找布局文件的路径，默认值为 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-applications.md#viewPath" target="_blank" rel="noopener noreferrer">视图路径</a> 下的 <code v-pre>layouts</code> 子目录。 如果 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-applications.md#viewPath" target="_blank" rel="noopener noreferrer">视图路径</a> 使用默认值， 默认的布局路径别名为<code v-pre>@app/views/layouts</code>。</p>
<p>该属性需要配置成一个目录或 路径 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-aliases.md" target="_blank" rel="noopener noreferrer">别名</a>。</p>
<h4 id="runtimepath-运行时临时文件路径" tabindex="-1"><a class="header-anchor" href="#runtimepath-运行时临时文件路径"><span>runtimePath（运行时临时文件路径）</span></a></h4>
<p>[[yii\base\Application::runtimePath]] 属性指定临时文件如日志文件、缓存文件等保存路径， 默认值为带别名的 <code v-pre>@app/runtime</code>。</p>
<p>可以配置该属性为一个目录或者路径 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-aliases.md" target="_blank" rel="noopener noreferrer">别名</a>， 注意应用运行时有对该路径的写入权限， 以及终端用户不能访问该路径因为临时文件可能包含一些敏感信息。</p>
<p>为了简化访问该路径，Yii 预定义别名 <code v-pre>@runtime</code> 代表该路径。</p>
<h4 id="viewpath-视图文件根目录" tabindex="-1"><a class="header-anchor" href="#viewpath-视图文件根目录"><span>viewPath（视图文件根目录）</span></a></h4>
<p>[[yii\base\Application::viewPath]] 属性指定视图文件的根目录，默认值为带别名的 <code v-pre>@app/views</code>， 可以配置它为一个目录或者路径 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-aliases.md" target="_blank" rel="noopener noreferrer">别名</a>.</p>
<h4 id="vendorpath-composer-安装包路径" tabindex="-1"><a class="header-anchor" href="#vendorpath-composer-安装包路径"><span>vendorPath（composer 安装包路径）</span></a></h4>
<p>[[yii\base\Application::vendorPath]] 属性指定 <a href="https://getcomposer.org/" target="_blank" rel="noopener noreferrer">Composer</a> 管理的供应商路径， 该路径包含应用使用的包括Yii框架在内的所有第三方库。 默认值为带别名的 <code v-pre>@app/vendor</code> 。</p>
<p>可以配置它为一个目录或者路径 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-aliases.md" target="_blank" rel="noopener noreferrer">别名</a>， 当你修改时，务必修改对应的 Composer 配置。</p>
<p>为了简化访问该路径，Yii 预定义别名 <code v-pre>@vendor</code> 代表该路径。</p>
<h4 id="enablecorecommands-是否启用核心命令" tabindex="-1"><a class="header-anchor" href="#enablecorecommands-是否启用核心命令"><span>enableCoreCommands（是否启用核心命令）</span></a></h4>
<p>[[yii\console\Application::enableCoreCommands]] 属性仅 [[yii\console\Application]] 控制台应用支持，用来指定是否启用 Yii 中的核心命令，默认值为 <code v-pre>true</code>。</p>
<h2 id="应用事件" tabindex="-1"><a class="header-anchor" href="#应用事件"><span>应用事件</span></a></h2>
<p>应用在处理请求过程中会触发事件，可以在配置文件配置事件处理代码， 如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'on beforeRequest'</span> <span class="token operator">=></span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>on eventName</code> 语法的用法在 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-configurations.md#configuration-format" target="_blank" rel="noopener noreferrer">Configurations</a> 一节有详细描述.</p>
<p>另外，在应用主体实例化后，你可以在 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/runtime-bootstrapping.md" target="_blank" rel="noopener noreferrer">引导启动</a> 阶段附加事件处理代码， 例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>base<span class="token punctuation">\</span>Application</span><span class="token operator">::</span><span class="token constant">EVENT_BEFORE_REQUEST</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="event-before-request-请求之前" tabindex="-1"><a class="header-anchor" href="#event-before-request-请求之前"><span>EVENT_BEFORE_REQUEST（请求之前）</span></a></h3>
<p>[[yii\base\Application::EVENT_BEFORE_REQUEST]] 事件在应用处理请求 <em>before</em> 之前，实际的事件名为 <code v-pre>beforeRequest</code>。</p>
<p>在事件触发前，应用主体已经实例化并配置好了， 所以通过事件机制将你的代码嵌入到请求处理过程中非常不错。 例如在事件处理中根据某些参数动态设置 [[yii\base\Application::language]] 语言属性。</p>
<h3 id="event-after-request-请求之后" tabindex="-1"><a class="header-anchor" href="#event-after-request-请求之后"><span>EVENT_AFTER_REQUEST（请求之后）</span></a></h3>
<p>[[yii\base\Application::EVENT_AFTER_REQUEST]] 事件在应用处理请求 <em>after</em> 之后但在返回响应 <em>before</em> 之前触发， 实际的事件名为 <code v-pre>afterRequest</code>。</p>
<p>该事件触发时，请求已经被处理完， 可以做一些请求后处理或自定义响应。</p>
<p>注意 [[yii\web\Response|response]] 组件在发送响应给终端用户时也会触发一些事件， 这些事件都在本事件 <em>after</em> 之后触发。</p>
<h3 id="event-before-action-运行动作之前" tabindex="-1"><a class="header-anchor" href="#event-before-action-运行动作之前"><span>EVENT_BEFORE_ACTION（运行动作之前）</span></a></h3>
<p>[[yii\base\Application::EVENT_BEFORE_ACTION]] 事件在每个 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-controllers.md" target="_blank" rel="noopener noreferrer">控制器动作</a> 运行<em>before</em>之前会被触发， 实际的事件名为 <code v-pre>beforeAction</code>.</p>
<p>事件的参数为一个 [[yii\base\ActionEvent]] 实例， 事件处理中可以设置[[yii\base\ActionEvent::isValid]] 为 <code v-pre>false</code> 停止运行后续动作， 例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'on beforeAction'</span> <span class="token operator">=></span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span>some condition<span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$event</span><span class="token operator">-></span><span class="token property">isValid</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>注意 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-modules.md" target="_blank" rel="noopener noreferrer">模块</a> 和 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-controllers.md" target="_blank" rel="noopener noreferrer">控制器</a> 都会触发 <code v-pre>beforeAction</code> 事件。 应用主体对象首先触发该事件，然后模块触发（如果存在模块），最后控制器触发。 任何一个事件处理中设置 [[yii\base\ActionEvent::isValid]] 设置为 <code v-pre>false</code> 会停止触发后面的事件。</p>
<h3 id="event-after-action-运行动作之后" tabindex="-1"><a class="header-anchor" href="#event-after-action-运行动作之后"><span>EVENT_AFTER_ACTION（运行动作之后）</span></a></h3>
<p>[[yii\base\Application::EVENT_AFTER_ACTION]] 事件在每个 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-controllers.md" target="_blank" rel="noopener noreferrer">控制器动作</a> 运行 <em>after</em> 之后会被触发， 实际的事件名为 <code v-pre>afterAction</code>。</p>
<p>该事件的参数为 [[yii\base\ActionEvent]] 实例， 通过 [[yii\base\ActionEvent::result]] 属性， 事件处理可以访问和修改动作的结果。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'on afterAction'</span> <span class="token operator">=></span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span>some condition<span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// 修改 $event->result</span></span>
<span class="line">        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>注意 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-modules.md" target="_blank" rel="noopener noreferrer">模块</a> 和 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-controllers.md" target="_blank" rel="noopener noreferrer">控制器</a> 都会触发 <code v-pre>afterAction</code> 事件。 这些对象的触发顺序和 <code v-pre>beforeAction</code> 相反，也就是说， 控制器最先触发，然后是模块（如果有模块），最后为应用主体。</p>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


